package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.TransInfoQueryMapper;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易信息信息正式表接口类
 * Created by yuhongwen
 */
public class TransInfoQueryMapperImpl implements TransInfoQueryMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);


    @Override
    public MchntOrderInfo selectMchntOrder(String instId, String mchntNo, String mchntOrderId, String origTraceNo, String origSysSeqNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfo");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntOrderInfo.setMchntNo(mchntNo);
        }
        if (null != mchntOrderId && !mchntOrderId.isEmpty()) {
            mchntOrderInfo.setMchntOrderId(mchntOrderId);
        }
        if (null != origTraceNo && !origTraceNo.isEmpty()) {
            mchntOrderInfo.setSysSeqNum(origTraceNo);
        }
        if (null != origSysSeqNo && !origSysSeqNo.isEmpty()) {
            mchntOrderInfo.setSysSeqNum(origSysSeqNo);
        }
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntOrderInfo, mchntOrderInfo);
        if (0 != iReturnCode) {
            rglog.error("商户订单查询失败");
            return null;
        }
//        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
//        if (null == objectList || objectList.isEmpty()) {
//            rglog.error("商户订单查询失败");
//            return null;
//        }
        return mchntOrderInfo;
    }


    @Override
    public List<PayOrderInfo> selectPayOrder(String instId, String mchntOrderId, String origTraceNo, String origSysSeqNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfo");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        if (null != mchntOrderId && !mchntOrderId.isEmpty()) {
            payOrderInfo.setMchntOrderId(mchntOrderId);
        }
        if (null != origTraceNo && !origTraceNo.isEmpty()) {
            payOrderInfo.setSysSeqNum(origTraceNo);
        }
        if (null != origSysSeqNo && !origSysSeqNo.isEmpty()) {
            payOrderInfo.setSysSeqNum(origSysSeqNo);
        }
        //int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr,payOrderInfo,payOrderInfo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        /*if (0 != iReturnCode){
            rglog.error("订单信息查询失败");
            return null;
        }*/
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("订单信息查询失败");
            return null;
        }
//        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
//        if (null == objectList || objectList.isEmpty()) {
//            rglog.error("订单信息查询失败");
//            return null;
//        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

}
